/**
 * 文件名称:  TBcUserContoller.java
 * 文件序号:  序号
 * 内容描述:  用户管理contoller层
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  MissJin
 * 创建日期:  2017/1/17/12:04
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */
package com.hongdee.rbac.user.contoller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;

import com.hongdee.rbac.dept.service.ITBcOrganService;
import com.hongdee.rbac.role.service.ITBcRoleService;
import com.hongdee.rbac.user.entity.TBcUser;
import com.hongdee.rbac.user.service.ITBcUserService;
import org.ietf.jgss.Oid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;

import java.util.List;
import java.util.Map;

/**
 * 名  称: 用户管理
 * 类  型: class
 * 描  述: 用户管理
 * 路  径: com.hongdee.rbac.user.contoller
 * 作  者: MissJin
 * 日  期: 2017/1/17/12:05
 */
@RestController
@RequestMapping("/rbac/user")
public class TBcUserContoller {
    @Autowired
    private ITBcUserService itBcUserService;

    @Autowired
    private ITBcOrganService organService;


    /**
     * @param uid    登录用户 ID
     * @param token  登录用户 Token
     * @param mobile 用户手机号
     * @param code   用户编码
     * @param name   用户姓名
     * @param oId    用户所在部门 ID
     * @return Map
     * @throws Exception 基类异常
     * @desc 根据部门 ID ，分页查询其所属用户
     * @author zhangkai
     * @date 2017/2/10
     * @time 11:05
     * @update_user
     * @update_time
     * @update_desc:
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "name", defaultValue = "") String name,
                    @RequestParam(name = "mobile", defaultValue = "") String mobile,
                    @RequestParam(name = "code", defaultValue = "") String code,
                    @RequestParam(name = "oId", defaultValue = "") String oId) throws Exception {
        //部门校验
        if (StringUtils.isEmpty(oId)) {
            throw new Exception("请先选择部门");
        }
        //判断是否显示
        boolean flag = organService.getChildDeptListByCompany(uid, oId);
        //判断是否可新增
//        boolean addflag = organService.getChildDeptListByCompany(uid, oId);
        Map map = itBcUserService.page(start, limit, oId, name, code, mobile);
        map.put("success", flag);
        map.put("msg", "分页查询成功！");

        map.put("addFlag", flag);

        return map;
    }

    /**
     * 方法名称: info
     * 方法描述: 查询用户明细
     * 参数名称: id用户主键
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/17/16:51
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择用户");
        }
        Map map = itBcUserService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    用户主键
     * @return ResultEntity
     * @throws Exception
     * @desc 查询分配给用户的资源
     */
    @RequestMapping("/res")
    @ResponseBody
    public ResultEntity res(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        List datalist = this.itBcUserService.res(id);
        res.setSuccess(true);
        res.setMsg("查询成功！");
        res.setDataList(datalist);
        return res;
    }

    /**
     * @param uid   登录用户主键
     * @param token 登录用户 token
     * @param id    用户主键
     * @param type  角色类型
     * @return ResultEntity
     * @throws Exception 异常基类
     * @desc 查询当前用户所拥有的角色
     */
    @RequestMapping("/role")
    @ResponseBody
    public ResultEntity role(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id,
                             @RequestParam(name = "type", defaultValue = "0") String type) throws Exception {
        ResultEntity res = new ResultEntity();
        List datalist = this.itBcUserService.role(id, type);
        res.setSuccess(true);
        res.setMsg("查询成功！");
        res.setDataList(datalist);
        return res;
    }

    /**
     * 方法名称: stop
     * 方法描述: 停用用户
     * 参数名称: id用户主键
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/17/17:45
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", required = false) String rId) throws Exception {
        String status = "0";
        String[] dl = rId.split(",");
        for (String id : dl) {
            this.itBcUserService.update(id, status);
        }
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }

    /**
     * 方法名称: start
     * 方法描述: 启用用户
     * 参数名称: id用户主键
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/17/17:45
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", required = false) String rId) throws Exception {
        String status = "1";
        String[] dl = rId.split(",");
        for (String id : dl) {
            this.itBcUserService.update(id, status);
        }
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 方法名称: save
     * 方法描述: 保存用户资料
     * 参数名称:
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/17/18:36
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcUser user) throws Exception {
        EntityUtils.valid(user);
        this.itBcUserService.saveOrUpdate(user);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(user);
        return res;
    }

    /**
     * 方法名称: saveRes
     * 方法描述: 保存用户资源
     * 参数名称:
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/17/18:37
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/saveRes")
    @ResponseBody
    public ResultEntity saveRes(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id", defaultValue = "") String id,
                                @RequestBody String[] resIds) throws Exception {

        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择用户");
        }
        this.itBcUserService.saveRes(id, resIds);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * @param uid     登录用户ID
     * @param token   登录用户token
     * @param id      用户 ID
     * @param type    角色类型
     * @param roleIds 角色 ID集合
     * @return ResultEntity
     * @throws Exception
     */
    @RequestMapping("/saveRole")
    @ResponseBody
    public ResultEntity saveRole(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "id", defaultValue = "") String id,
                                 @RequestParam(name = "type", defaultValue = "0") String type,
                                 @RequestBody String[] roleIds) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择用户");
        }
        this.itBcUserService.saveRole(id, type, roleIds);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * @param uid      登录用户 ID
     * @param token    登录用户 Token
     * @param id       用户主键
     * @param password 用户密码
     * @return
     * @throws Exception
     * @desc 用户修改密码
     * @author zhangkai
     * @date 2017/2/10
     * @time 13:17
     * @update_user
     * @update_time
     * @update_desc:
     */
    @RequestMapping("/savePass")
    @ResponseBody
    public ResultEntity savePass(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "id", defaultValue = "") String id,
                                 @RequestParam(name = "pass", defaultValue = "") String password) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择用户");
        }
        this.itBcUserService.savePassWord(id, password);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("重置成功");
        return res;
    }

    /**
     * @param
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "role", defaultValue = "") String role) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        return this.itBcUserService.combo(oid,role);
    }

    /**
     * 公司下所有人员
     *
     * @author 徐钦勇
     */
    @RequestMapping("/allUser")
    @ResponseBody
    public Map AllUser(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "uname", defaultValue = "") String uname,
                       @RequestParam(name = "companyId",defaultValue = "") String companyId,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "999") String limit) throws Exception {
        return this.itBcUserService.allUser(uid, uname,companyId, start, limit);
    }

    /**
     * 公司下有任务处理权限的人员
     *
     * @author 徐钦勇
     */
    @RequestMapping("/hasQxUser")
    @ResponseBody
    public Map hasQxUser(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "uname", defaultValue = "") String uname,
                         @RequestParam(name = "start", defaultValue = "") String start,
                         @RequestParam(name = "limit", defaultValue = "") String limit,
                         @RequestParam(name = "pcode", defaultValue = "") String pcode,
                         @RequestParam(name = "code", defaultValue = "") String code,
                         @RequestBody List<Map> data) throws Exception {
        return this.itBcUserService.hasQxUser(uid, uname, start, limit, data, pcode, code);
    }

    /****
     * 基础配置-updateDept
     * @user zhgk
     * @date 2017-09-15
     */
    @RequestMapping("/updateDept")
    @ResponseBody
    public ResultEntity updateDept(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "userId", defaultValue = "") String userId,
                                   @RequestParam(name = "deptId", defaultValue = "") String deptId,
                                   @RequestBody List<Map> data) throws Exception {
        if (StringUtils.isEmpty(deptId)) {
            throw new Exception("请选择部门");
        }
        this.itBcUserService.updateDept(userId, deptId);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("重置成功");
        return res;
    }

    @RequestMapping("/allUsers")
    @ResponseBody
    public Map allusers(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token)throws Exception{
        Map map = new HashMap();
        map = this.itBcUserService.allusers(uid);
        map.put("success",true);
        map.put("msg","查询成功");
        return map;
    }


}
